package com.company.cloud.admin.modules.admin.service.impl;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.company.cloud.admin.constant.PlatformAdminErrorEnum;
import com.company.cloud.admin.modules.admin.controller.req.RoleSaveParam;
import com.company.cloud.admin.modules.admin.controller.req.RoleUpdateParam;
import com.company.cloud.admin.modules.admin.service.RoleMgrService;
import com.company.cloud.admin.modules.admin.utils.AdminSNUtils;
import com.company.cloud.admin.modules.base.entity.RoleInfo;
import com.company.cloud.admin.modules.base.entity.RolePermissionInfo;
import com.company.cloud.admin.modules.base.entity.UserRoleInfo;
import com.company.cloud.admin.modules.base.service.RoleInfoService;
import com.company.cloud.admin.modules.base.service.RolePermissionInfoService;
import com.company.cloud.admin.modules.base.service.UserRoleInfoService;
import com.company.cloud.common.asserts.ValidatorUtils;
import com.company.cloud.common.bean.LoginSimpleUserInfo;
import com.company.cloud.common.constant.BaseConstant;
import com.company.cloud.common.exception.BusinessException;

@Service
public class RoleMgrServiceImpl implements RoleMgrService {

	
	@Autowired
	private RolePermissionInfoService rolePermissionInfoService;
	@Autowired
	private UserRoleInfoService userRoleInfoService;
	@Autowired
	private RoleInfoService roleInfoService;
	
	
	
	@Transactional
	@Override
	public void save(RoleSaveParam params, LoginSimpleUserInfo userInfo) {
		ValidatorUtils.validate(params);
		// 角色
		RoleInfo roleInfo = new RoleInfo();
		roleInfo.setCode(params.getCode());
		roleInfo.setName(params.getName());
		roleInfo.setRemark(params.getRemark());
		roleInfo.setIsSys(BaseConstant.YNEnum.NO.getCode());
		roleInfo.addCreateParam(userInfo);
		roleInfo.addEditParam(userInfo);
		roleInfoService.save(roleInfo);
		//角色和权限
		if(!CollectionUtils.isEmpty(params.getPermissionCodes())) {
			Set<RolePermissionInfo> rolePerSet = new HashSet<RolePermissionInfo>();
			for(String perCode:params.getPermissionCodes()) {
				if(!StringUtils.isEmpty(perCode)) {
					RolePermissionInfo rolePer = new RolePermissionInfo();
					rolePer.setRoleCode(roleInfo.getCode());
					rolePer.setPerCode(perCode);
					rolePer.addCreateParam(userInfo);
					rolePer.addEditParam(userInfo);
					rolePerSet.add(rolePer);
				}
			}
			if (rolePerSet.size() > 0) {
				rolePermissionInfoService.saveBatch(rolePerSet);
			}
		}
	}


	@Transactional
	@Override
	public void update(RoleUpdateParam params, LoginSimpleUserInfo userInfo) {
		ValidatorUtils.validate(params);
		// 角色
		RoleInfo roleInfo = roleInfoService.getById(params.getCode());
		if ( null == roleInfo ) {
			throw new BusinessException(PlatformAdminErrorEnum.ROLE_NULL);
		}
		if( BaseConstant.YNEnum.YES.getCode().equals(roleInfo.getIsSys()) ) {
			throw new BusinessException(PlatformAdminErrorEnum.ROLE_IS_MANAGER);
		}
		roleInfo.setName(params.getName());
		roleInfo.setRemark(params.getRemark());
		roleInfo.addEditParam(userInfo);
		roleInfoService.updateById(roleInfo);

		// 角色和权限
		rolePermissionInfoService.remove(new QueryWrapper<RolePermissionInfo>()
											.eq("role_code", roleInfo.getCode())
										);
		
		//角色和权限
		if(!CollectionUtils.isEmpty(params.getPermissionCodes())) {
			Set<RolePermissionInfo> rolePerSet = new HashSet<RolePermissionInfo>();
			for(String perCode:params.getPermissionCodes()) {
				if(!StringUtils.isEmpty(perCode)) {
					RolePermissionInfo rolePer = new RolePermissionInfo();
					rolePer.setRoleCode(roleInfo.getCode());
					rolePer.setPerCode(perCode);
					rolePer.addCreateParam(userInfo);
					rolePer.addEditParam(userInfo);
					rolePerSet.add(rolePer);
				}
			}
			if (rolePerSet.size() > 0) {
				rolePermissionInfoService.saveBatch(rolePerSet);
			}
		}
		
		//角色关联的用户退出
		List<UserRoleInfo> userRoleList= userRoleInfoService.list(new QueryWrapper<UserRoleInfo>()
				.eq("role_code", roleInfo.getCode()));
		
	}


	@Override
	public List<UserRoleInfo> delete(String code, LoginSimpleUserInfo userInfo) {
		if(StringUtils.isEmpty(code)) {
			throw new BusinessException(PlatformAdminErrorEnum.ROLE_CODE_NULL);
		}
		RoleInfo roleInfo = roleInfoService.getById(code);
		if (null == roleInfo ) {
			throw new BusinessException(PlatformAdminErrorEnum.ROLE_NULL);
		}
		if(BaseConstant.YNEnum.YES.getCode().equals(roleInfo.getIsSys()) ) {
			throw new BusinessException(PlatformAdminErrorEnum.ROLE_IS_MANAGER);
		}
		//删除角色
		roleInfoService.removeById(code);
		
		//角色关联的用户退出
		List<UserRoleInfo> userRoleList= userRoleInfoService.list(new QueryWrapper<UserRoleInfo>()
				.eq("role_code", roleInfo.getCode())
				);
		
		//删除角色权限
		rolePermissionInfoService.remove(new QueryWrapper<RolePermissionInfo>()
				.eq("role_code", roleInfo.getCode())
			);
		
		//删除角色用户
		userRoleInfoService.remove(new QueryWrapper<UserRoleInfo>()
					.eq("role_code", roleInfo.getCode())
				);
		
		return userRoleList;
	}
}
